<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>品类操作</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .category-container {
            padding: 20px;
        }

        .lay-form {
            padding: 20px;
        }
        .category-container {
            background-color: #fff;
        }
        .util-box,
        .search-select {
            align-items: center;
            display: flex;
            justify-content: flex-start;
        }
        .util-box .layui-form-item {
            margin-bottom: 0;
        }
        .util-box .layui-form-label {
            width: 130px;
        }
        .util-box .layui-input-block {
            margin-left: 130px;
        }
    </style>
</head>

<body>
    <div class="category-container">
         <!-- 搜索 -->
         <div>
            <div class="layui-inline">
                <input class="layui-input" name="categoryName" id="demoReload" placeholder="请输入搜索品类名称" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>

        <table lay-filter="categoryTable" class="layui-hide" id="categoryTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="util-box">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="xj">批量下架</button>
            <button class="layui-btn layui-btn-sm" lay-event="sj">批量上架</button>
        </div>
        <div class="search-select">
            <!-- 筛选 -->
            <div class="layui-form-item" id="status">
                <label class="layui-form-label">激活状态筛选：</label>
                <div class="layui-input-block">
                  <select name="stop" lay-filter="select">
                    <option value="">全部</option>
                    <option value="0">上架</option>
                    <option value="1">下架</option>
                  </select>
                </div>
            </div>
    
        </div>
    </div>
    
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
<script src="../utils.js"></script>
<script>

    layui.use(['table', 'upload', 'form', 'jquery'], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            $ = layui.jquery;

        var url = window.baseUrl + '/goods/getCategoryList';

        var tab = table.render({
            elem: '#categoryTable'
            , toolbar: "#toolbarDemo"
            , url: url
            , page: false
            , headers: {
                Authorization: localStorage.token
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                res.data.data.forEach(el => {
                    el.stop = el.stop == 0 ? true : false; 
                })
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            ,request: {
                pageName: 'start' //页码的参数名称，默认：page
                ,limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {type:'checkbox'}
                , { field: 'id', width: 80, title: 'ID', sort: true }
                , { field: 'categoryName', title: '品类名称' }
                , { field: 'stop', title: '状态', templet: function(res) {
                    return '<span>'+ (res.stop ? '上架' : "下架") +'</span>'
                }}
                , { field: 'createTime', title: '创建时间' }
                , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 80 }
            ]]
            , even: true
            , page: true
        });

        //工具栏事件
        table.on('toolbar(categoryTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    //页面层
                    showLayer();
                    $("#categoryId").val("");
                    break;
                case 'xj':
                    var checkStatus = table.checkStatus('categoryTable')
                        ,data = checkStatus.data;
                        data.forEach(el => {
                            el.stop = 1;

                            ajax({
                                path: "/goods/updateCategory",
                                data: {id: el.id, stop: el.stop},
                                success: function (res) {
                                    table.reload("categoryTable");
                                }
                            }, $)
                            layer.msg("批量下架成功！")
                        })
                    break;
                case 'sj':
                    var checkStatus = table.checkStatus('categoryTable')
                        ,data = checkStatus.data;
                        data.forEach(el => {
                            el.stop = 0;

                            ajax({
                                path: "/goods/updateCategory",
                                data: {id: el.id, stop: el.stop},
                                success: function (res) {
                                    table.reload("categoryTable");
                                }
                            }, $)
                            layer.msg("批量上架成功！")
                        })
                    break;
            };
        });


        $('#search').on('click', function(){
            //执行重载
            let val = $("#demoReload").val();
            var where = val ? { categoryName: val} : null;
            
            table.reload('categoryTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                ,where: where
            });

        });


        // 筛选状态
        form.on("select(select)", function(e){
            table.reload('categoryTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                ,where: {
                    stop: e.value
                }
            });
            
            setTimeout(function() {
                $("#status .layui-select-title input").attr("placeholder", e.value == '0' ? "上架" : (e.value == "1" ? "下架" : "全部"))
            }, 300)
            
        })

        // 编辑和删除
        table.on("tool(categoryTable)", function(data) {
            switch(data.event) {
                case "edit":
                    showLayer();
                    form.val('scategoryForm', data.data);
                    break;
            }
        })


        function showLayer() {
            layer.open({
                type: 1,
                title: "添加/编辑品类",
                area: ['550px', '260px'], //宽高
                content: `
                        <form class="layui-form lay-form" lay-filter="scategoryForm">
                            <div class="layui-form-item">
                                <label class="layui-form-label">品类名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="categoryName" lay-verify="required" lay-reqtext="品类名称是必填项，岂能为空？" placeholder="请输入名称" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" checked="" name="stop" lay-skin="switch" lay-filter="switchTest" lay-text="上架|下架">
                                </div>
                            </div>

                            <input type="hidden" name="id" id="categoryId" value>

                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="categorySubmit">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </form>
                    `
            });

            form.render();


            // form 提交
            form.on("submit(categorySubmit)", function (el) {

                if(el.field.stop) {
                    el.field.stop = 0;
                } else {
                    el.field.stop = 1;
                }

                // 编辑
                if (el.field.id) {
                    ajax({
                        path: "/goods/updateCategory",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("编辑成功");
                            table.reload("categoryTable");
                        }
                    }, $)
                } 
                // 添加
                else {
                    ajax({
                        path: "/goods/addCategory",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("添加成功");
                            table.reload("categoryTable");
                        }
                    }, $)
                }
                

                return false;
            })
        }
    });
</script>

</html>